import sched, time, datetime, timeit
from time import gmtime, strftime
from threading import Thread
from Strategy.pairs_trade_try import trade_pairs_try
from Strategy.maket_maker import market_maker
from Strategy.config import STATIC
from MarketData.const import CONST

from GUI.output import PATH
import logging, os
from Server.Message.send import send_sms
from PlaceOrder.order import newOrder

path_of_current_dir = os.path.abspath(os.path.dirname(os.getcwd()))
app_log_path = '%s/%s_error_%s.log' % (path_of_current_dir, 'app', datetime.datetime.now().strftime('%Y_%m_%d_%H_%M_%S'))

logging.basicConfig(filename=app_log_path,
                    format='%(asctime)s %(message)s',
                    datefmt='%Y/%m/%d %H:%M:%S',
                    level=logging.INFO)


def run(platform, currency):

    logging.info([platform, currency, strftime("%Y-%m-%d %H:%M:%S", gmtime())])

    try:
        time_now = timeit.default_timer()

        STATIC.PlatformList_valid = []
        for platf in CONST.PlatformList:
            if time_now - STATIC.time_error[platf] > 9000:
                STATIC.PlatformList_valid.append(platf)

        for platf in platform:
            if time_now - STATIC.time_error[platf] < 9000:
                platform.remove(platf)

        market_maker(platform, currency)

    except Exception as e:
        logging.info(str(e))

    time.sleep(3)
    logging.info("")
    logging.info("")
    logging.info("")
    logging.info("")
    logging.info("*****************************************************************************************************")
    logging.info("")
    logging.info("")
    logging.info("")
    logging.info("")


STATIC.price['BTC', 'BTC'] = 1
STATIC.price['CNY', 'USD'] = 1.0/6.4
STATIC.price['USDT', 'USD'] = 1
for platf in CONST.PlatformList:
    STATIC.time_error[platf] = -9000

while True:
    platform = ["hitbtc", "bigone", "bitfinex", "bittrex", "huobi"]
    # platform = ["huobi"]

    currency = ["ETH", "ETH", "BTC"]
    run(platform, currency)

    currency = ["EOS", "EOS", "BTC"]
    run(platform, currency)

    currency = ["EOS", "EOS", "ETH"]
    run(platform, currency)

    currency = ["ETH", "ETH", "USDT"]
    run(platform, currency)





